package com.jshoperxms.components.wechat.templatemsg;

import com.jshoperxms.components.wechat.WxTokenUtils;
import com.jshoperxms.controller.utils.statickey.TagWords;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by sdywcd on 2018/10/29.
 * Des:
 *
 */
@Component
public class TemplateMsgController {


    @Autowired
    private WxTokenUtils wxTokenUtils;

    private Map<String,Object> responsedata=new HashMap<>();



    public void sendNoticToLangDuAuthorThanReceiveComment(String formId,String commentContent,String commentMember,String toMemberid,String recordid){

        String accessToken=wxTokenUtils.getToken();

        String uri="https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token="+accessToken;
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("keyword1", toJson(commentContent));
        jsonObject.put("keyword2",toJson(commentMember));
        jsonObject.put("keyword3",toJson("这是来自'朗读能手'的评论提醒，继续加油朗读哦"));

        JSONObject json=new JSONObject();
        json.put("touser",toMemberid);
        json.put("template_id",Template.FINISHCOMMENT);
        json.put("form_id",formId);
        json.put("page","pages/index/trecordinfo?recordid="+recordid);
        json.put("data",jsonObject);

        HttpClient client=new DefaultHttpClient();
        HttpPost post=new HttpPost(uri);
        post.setHeader("Content-Type","application/json");
        String result="";
        try{
            StringEntity stringEntity=new StringEntity(json.toString(),"utf-8");
            stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
            post.setEntity(stringEntity);

            HttpResponse httpResponse=client.execute(post);
            InputStream inputStream=httpResponse.getEntity().getContent();
            BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
            StringBuilder stringBuilder=new StringBuilder();
            String line=null;
            while ((line=reader.readLine())!=null){
                stringBuilder.append(line+"\n");
            }
            inputStream.close();
            result=stringBuilder.toString();
            if (httpResponse.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
                responsedata.put(TagWords.SUCCESS,true);
                responsedata.put("result",result);
            }
            System.out.print("发送消息结果==="+result);

        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public JSONObject toJson(String value){
        JSONObject json = new JSONObject();
        json.put("value", value);
        json.put("color", "#173177");//消息字体颜色
        return json;
    }
}
